﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PolePosition.v3.Desktop.Editors.Controls
{
    public partial class Images : DialogEditBase
    {
      
        public Images()
        {
            InitializeComponent();
        }

        private DataTable m_Images;
        private int m_Id;
        private int m_ImageCount;
        private Common.Enumerations.DataTypes m_DataType;

        public void LoadImages(int id, Common.Enumerations.DataTypes dataType)
        {
            m_Id = id;
            m_DataType = dataType;

            m_Images = new DataTable();
            m_Images.Columns.Add("ImageName", typeof(string));
            m_Images.Columns.Add("ImagePath", typeof(string));

            backgroundWorkerImages.RunWorkerAsync();
        }

        public string GetDirectoryName(int parentId)
        {
            //copied from old PPI client
            int Start = 0;
            int End = 99;
            string _Directory = "";
            bool _FoundPosition = false;
            while (!_FoundPosition)
            {
                if (parentId > Start && parentId > End)
                {
                    Start += 100;
                    End += 100;
                }
                else
                {
                    _FoundPosition = true;
                    _Directory = Start.ToString() + "-" + End.ToString();
                }
            }
            return _Directory;
        }

        private void backgroundWorkerImages_DoWork(object sender, DoWorkEventArgs e)
        {
            //Pull Url from settings
            string _baseURL;
            _baseURL = Program.GetSettingValue(Common.Constants.Settings.BaseImageStorageUrl);

            int _Cntr = 1;
            m_ImageCount = 0;
            //load all the results in a datatable that way no matter the source the the display
            //logic is the same
            switch (m_DataType)
            {
                case PolePosition.v3.Common.Enumerations.DataTypes.AssetsXmission:
                    ProjectCommon2.Generics.EntityCollection<EntityLibrary.UtilityAssetXMissionStructuresImageEntity> _XmissionAssetImages;
                    _XmissionAssetImages = ServiceProxy.UtilityService.GetXmissionAssetPoleImages(m_Id);

                    if (_XmissionAssetImages == null || _XmissionAssetImages.Count() != 0)
                    {
                        m_ImageCount = _XmissionAssetImages.Count();
                        foreach (EntityLibrary.UtilityAssetXMissionStructuresImageEntity _Image in _XmissionAssetImages)
                        {
                            DataRow _Row = m_Images.NewRow();
                            _Row.SetField<string>("ImageName", string.Format("Image {0}", _Cntr.ToString()));
                            _Row.SetField<string>("ImagePath", string.Format("{0}Utility_Asset_XMissionStructures/{1}/{2}", _baseURL, GetDirectoryName(_Image.Utility_Asset_XMissionStructures_PoleFK.Value), _Image.Filename));
                            m_Images.Rows.Add(_Row);
                            _Cntr++;
                        }
                    }
                    break;
                case PolePosition.v3.Common.Enumerations.DataTypes.AssetsDistribution:
                    ProjectCommon2.Generics.EntityCollection<EntityLibrary.UtilityAssetDistributionsImageEntity> _DistributionAssetImages;
                    _DistributionAssetImages = ServiceProxy.UtilityService.GetDistributionAssetPoleImages(m_Id);

                    if (_DistributionAssetImages == null || _DistributionAssetImages.Count() != 0)
                    {
                        m_ImageCount = _DistributionAssetImages.Count();
                        foreach (EntityLibrary.UtilityAssetDistributionsImageEntity _Image in _DistributionAssetImages)
                        {
                            DataRow _Row = m_Images.NewRow();
                            _Row.SetField<string>("ImageName", string.Format("Image {0}", _Cntr.ToString()));
                            _Row.SetField<string>("ImagePath", string.Format("{0}Utility_Asset_Distribution/{1}/{2}", _baseURL, GetDirectoryName(_Image.Utility_Asset_DistributionFK.Value), _Image.Filename));
                            m_Images.Rows.Add(_Row);
                            _Cntr++;
                        }
                    }
                    break;
                case PolePosition.v3.Common.Enumerations.DataTypes.AssetsMeters:
                    break;
                case PolePosition.v3.Common.Enumerations.DataTypes.ProjectsXmissionInspection:
                    ProjectCommon2.Generics.EntityCollection<EntityLibrary.VendorInspVisitXmissionStructuresImageEntity> _XmissionInspectionImages;
                    _XmissionInspectionImages = ServiceProxy.UtilityService.GetXmissionInspectionProjectPoleImages(m_Id);

                    if (_XmissionInspectionImages == null || _XmissionInspectionImages.Count() != 0)
                    {
                        m_ImageCount = _XmissionInspectionImages.Count();
                        foreach (EntityLibrary.VendorInspVisitXmissionStructuresImageEntity _Image in _XmissionInspectionImages)
                        {
                            DataRow _Row = m_Images.NewRow();
                            _Row.SetField<string>("ImageName", string.Format("Image {0}", _Cntr.ToString()));
                            _Row.SetField<string>("ImagePath", string.Format("{0}Inspection/{1}/{2}", _baseURL, GetDirectoryName(_Image.Vendor_InspVisit_XMissionStructure_PoleFk.Value), _Image.Filename));
                            m_Images.Rows.Add(_Row);
                            _Cntr++;
                        }
                    }
                    break;
                case PolePosition.v3.Common.Enumerations.DataTypes.ProjectsXmissionGpsVisit:
                    ProjectCommon2.Generics.EntityCollection<EntityLibrary.VendorVisitXMissionStructuresImageEntity> _XmissionGpsImages;
                    _XmissionGpsImages = ServiceProxy.UtilityService.GetXmissionGpsProjectPoleImages(m_Id);

                    if (_XmissionGpsImages == null || _XmissionGpsImages.Count() != 0)
                    {
                        m_ImageCount = _XmissionGpsImages.Count();
                        foreach (EntityLibrary.VendorVisitXMissionStructuresImageEntity _Image in _XmissionGpsImages)
                        {
                            DataRow _Row = m_Images.NewRow();
                            _Row.SetField<string>("ImageName", string.Format("Image {0}", _Cntr.ToString()));
                            _Row.SetField<string>("ImagePath", string.Format("{0}GPS/{1}/{2}", _baseURL, GetDirectoryName(_Image.Vendor_Visit_XMissionStructureFK.Value), _Image.Filename));
                            m_Images.Rows.Add(_Row);
                            _Cntr++;
                        }
                    }
                    break;
                case PolePosition.v3.Common.Enumerations.DataTypes.ProjectsDistributionInspection:
                    ProjectCommon2.Generics.EntityCollection<EntityLibrary.VendorDistributionImageEntity> _DistributionInspectionImages;
                    _DistributionInspectionImages = ServiceProxy.UtilityService.GetDistributionInspectionProjectPoleImages(m_Id);

                    if (_DistributionInspectionImages == null || _DistributionInspectionImages.Count() != 0)
                    {
                        m_ImageCount = _DistributionInspectionImages.Count();
                        foreach (EntityLibrary.VendorDistributionImageEntity _Image in _DistributionInspectionImages)
                        {
                            DataRow _Row = m_Images.NewRow();
                            _Row.SetField<string>("ImageName", string.Format("Image {0}", _Cntr.ToString()));
                            _Row.SetField<string>("ImagePath", string.Format("{0}DistributionInspection/{1}/{2}", _baseURL, GetDirectoryName(_Image.Vendor_Distribution_PoleFk.Value), _Image.Filename));
                            m_Images.Rows.Add(_Row);
                            _Cntr++;
                        }
                    }
                    break;
                case PolePosition.v3.Common.Enumerations.DataTypes.ProjectsDistributionGpsVisit:
                    break;
                case PolePosition.v3.Common.Enumerations.DataTypes.WorkQueueXmission:
                    break;
                case PolePosition.v3.Common.Enumerations.DataTypes.WorkQueueDistribution:
                    break;
                case PolePosition.v3.Common.Enumerations.DataTypes.WorkQueueMeter:
                    break;
                case PolePosition.v3.Common.Enumerations.DataTypes.None:
                    break;
                case PolePosition.v3.Common.Enumerations.DataTypes.Custom:
                    break;
                default:
                    break;
            }
        }

        private void backgroundWorkerImages_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            foreach (DevComponents.DotNetBar.BaseItem _Button in itemPanelImages.Items)
            {
                if (_Button.Tag != null && !string.IsNullOrEmpty(_Button.Tag.ToString()))
                {
                    int _ImageNumber;
                    _ImageNumber = int.Parse(_Button.Tag.ToString());

                    if (_ImageNumber > m_ImageCount)
                        _Button.Enabled = false;
                    else
                        _Button.Enabled = true;
                }
            }

            //load the first image
            if (m_ImageCount > 0)
                buttonItemImage_Click(buttonItemImage1, EventArgs.Empty);
        }

        private void buttonItemImage_Click(object sender, EventArgs e)
        {
            //in the tag of each button is the image number find the corresponding row in the table and you 
            //know what image to show

            int _ImageNumber;
            _ImageNumber = int.Parse((sender as DevComponents.DotNetBar.ButtonItem).Tag.ToString());

            string _ImageViewerUrl;
            _ImageViewerUrl = Program.GetSettingValue(Common.Constants.Settings.ImageViewerUrl);

            string _url = string.Format("{0}?HEIGHT=322px&WIDTH=429px&PATH={1}", _ImageViewerUrl, System.Web.HttpUtility.UrlEncode(m_Images.Rows[_ImageNumber - 1].Field<string>("ImagePath")));

            Uri _Uri = new Uri(_url);

            if (webBrowserImage.Url != _Uri)
                webBrowserImage.Url = new Uri(_url);
        }

        private void itemPanelImages_ItemClick(object sender, EventArgs e)
        {
            if (sender == buttonItemAddNew)
                return;

            //alow only one selected
            foreach (object _Control in itemPanelImages.Items)
            {
                if (_Control != sender && (_Control as DevComponents.DotNetBar.ButtonItem) != null)
                    (_Control as DevComponents.DotNetBar.ButtonItem).Checked = false;
                else
                    (_Control as DevComponents.DotNetBar.ButtonItem).Checked = true;
            }
        }
    }
}
